<?php
include_once('admin.inc.php');
$advModel = new Model("advertise");
$smarty = new Tpl();

$act = get_param("do");
switch( $act ){
	case "banners":
		// 首页大图广告
		$list = $advModel->where('type=1')->order('`order`')->get();
		if( DEBUG ){
			echo 'banners: ',$advModel->get_sql(),'<hr>';
			dump($list);
		}
		$smarty->assign('pageTitle', '首页广告大图');
		$smarty->assign("list", $list);
		$smarty->display('admin/banners.html');
			
	break;
	case "product":
		// 首页产品列表
		$list = $advModel->where('type=2')->order('`order`')->get();
		if( DEBUG ){
			echo 'banners: ',$advModel->get_sql(),'<hr>';
			dump($list);
		}
		$smarty->assign("list", $list);
		$smarty->display('admin/banners.html');
			
	break;
	case "featured":
		// 首页精选产品
		$list = $advModel->where('type=3')->order('`order`')->get();
		if( DEBUG ){
			echo 'banners: ',$advModel->get_sql(),'<hr>';
			dump($list);
		}
		$smarty->assign("list", $list);
		$smarty->display('admin/banners.html');
			
	break;
	case "addBanner":
		$smarty->assign('type', 1);
		$smarty->assign('pageTitle', '添加首页大图广告');
		$smarty->display('admin/addAdv.html');
	break;
	case "addProduct":
		$smarty->assign('type', 2);
		$smarty->assign('pageTitle', '添加首页产品列表');
		$smarty->display('admin/addAdv.html');
	break;
	case "addFeatured":
		$smarty->assign('type', 3);
		$smarty->assign('pageTitle', '添加首页精选产品');
		$smarty->display('admin/addAdv.html');
	break;	
	case "addAdv":
		// 添加首页大图广告
		if( get_param('type') ){
			include_once(__LIB__.'Upload.class.php');
			$upCfg = array(
				// 文件大小：3M	
				'max_size'	=> 3*1024*1024,
				'safe_ext'	=> array('jpg','jpeg','png','gif','bmp','tiff'),
				'save_path'	=> __UPLOAD__.'advertise/'
				);
			$upSaveName = 'adv_'.get_param('type').'_';
			$up = new Upload('file',$upCfg);
			$rsUp = $up->save($upSaveName);
			if( $rsUp ){
				$rsUp = json_decode($rsUp,true);
				$data = array();
				$data['file'] = $rsUp[0]['saveName']; 
				$data['title'] = get_param('desc');
				$data['subtitle'] = get_param('subtitle');
				$data['order'] = get_param('order');
				$data['type'] = get_param('type');
				$data['link'] = get_param('link');
				//成功后跳转地址，根据type类型判断
				$from = array(1=>'banners', 2=>'product', 3=>'featured');
				$rs = $advModel->value($data)->add();
				if( DEBUG ){
					echo 'addAdv: ',$advModel->get_sql(),'<hr>';
					dump($rs);
				}
				if( $rs>0 ){
					msg('添加成功','advertise.php?do='.$from[$data['type']]);
				}else{
					@unlink(__UPLOAD__.'advertise/'.$rsUp[0]['saveName']);
					msg('添加失败：写入数据库失败',-1);
				}
			}else{
				$msg = '添加失败：上传文件失败';
				if( $up->get('error') ){
					$msg .= ','.$up->get('error');
				}
				msg($msg);	
			}
		}
	break;
	case "delAdv":
		//删除广告
		$id = intval(get_param('id'));
		$return = array('status'=>'fail','info'=>'删除失败');
		if( empty($id) ) ajax_return($return);
		$rs = $advModel->where("id=$id")->del();
		if( DEBUG ){
			echo 'delAdv: ',$advModel->get_sql(),'<hr>';
		}
		if( $rs ){
			$return = array('status'=>'success','info'=>'删除成功');
			ajax_return($return);
		}else{
			ajax_return($return);
		}
	break;
	case 'editAdv':
		//修改
		$id = intval(get_param('id'));
		if( empty($id) ){
			if( get_param('ajax') ){
				ajax_return(array('status'=>'failed','info'=>'非法id'));
			}else{
				msg('非法id',-1);
			}
		}
		$advinfo = $advModel->where("id=$id")->get_one();
		if( get_param('submit') ){
			if( !empty($_FILES['file']['name']) ){
				include_once(__LIB__.'Upload.class.php');
				$upCfg = array(
					// 文件大小：3M	
					'max_size'	=> 3*1024*1024,
					'safe_ext'	=> array('jpg','jpeg','png','gif','bmp','tiff'),
					'save_path'	=> __UPLOAD__.'advertise/'
					);
				$upSaveName = 'adv_'.get_param('type').'_';
				$up = new Upload('file',$upCfg);
				$rsUp = $up->save($upSaveName);
				if( !$rsUp ){
					$msg = '添加失败：上传文件失败';
					if( $up->get('error') ){
						$msg .= ','.$up->get('error');
					}
					msg($msg);
				}
				$rsUp = json_decode($rsUp,true);
				@unlink(__ROOT__.'var/advertise/'.$advinfo['file']);
			}
			$data = array();
			if( $rsUp ) $data['file'] = $rsUp[0]['saveName']; 
			$data['title'] = get_param('desc');
			$data['subtitle'] = get_param('subtitle');
			$data['order'] = (int)get_param('order');
			$data['type'] = get_param('type');
			$data['link'] = get_param('link');
			$rs = $advModel->where("id=$id")->value($data)->update();
			$from = array(1=>'banners', 2=>'product', 3=>'featured');
			if($rs){
				msg('修改成功','advertise.php?do='.$from[$data['type']]);
			}else{
				msg('修改失败','advertise.php?do='.$from[$data['type']]);
			}
			exit;
		}
		$smarty->assign('advinfo',$advinfo);
		$smarty->assign('type', $advinfo['type']);
		$smarty->assign('do', 'editAdv');
		$smarty->display('admin/addAdv.html');
		
	break;
}
